var app = angular.module('myApp', []);

app.controller('searchTaskCtrl', ['$scope','$compile','$http','userPositionFactory',function ($scope, $compile,$http,userPositionFactory) {

    $scope.markerList=[];

    $scope.currentPos={
        latitude:31.194162,
        longitude:121.59525
    }

    $scope.taskList=[
        {
            latitude:31.194,
            longitude:121.595,
            user:{
                userId:1,
                name:'李文海1',
                headImage:'img/user_icon.png',
                reputation:3000,
                credit:2000,
                finish:10,
                publish:5

            },
            task:{
                taskId:1,
                name:'帮取快递',
                type:'GE',
                description:'阿康烧烤旁4栋7号至软件楼401',
                reward:100,
                startTime:'2017-1-20 08:00',
                endTime:'2017-1-20 18:00'
            }
        },
        {
            latitude:31.19417,
            longitude:121.594,
            user:{
                userId:1,
                name:'李文海2',
                headImage:'img/user_icon.png',
                reputation:200,
                credit:100,
                finish:10,
                publish:5
            },
            task:{
                taskId:1,
                name:'帮取快递',
                type:'GE',
                description:'阿康烧烤旁4栋7号至软件楼401',
                reward:100,
                startTime:'2017-1-20 08:00',
                endTime:'2017-1-20 18:00'
            }
        }
    ];

    $('#search-map').css('height',window.screen.height-127 );

    //$(document).ready(function (){
    //    $(".search-task-ul").initslide(
    //        {
    //        }
    //    );//initialize itemslide
    //});


    // 百度地图API功能
    var map = new BMap.Map("search-map");

    //利用百度地图API进行地理位置坐标转换（需要引入convertor.js这一文件）
    var gpsPoint = new BMap.Point($scope.currentPos.longitude,$scope.currentPos.latitude);
    BMap.Convertor.translate(gpsPoint, 0, function (point) {
        var geoc = new BMap.Geocoder();
        geoc.getLocation(point, function (rs) {
            map.centerAndZoom(point, 18);
            map.addOverlay(new BMap.Marker(point)) ;
            var addComp = rs.addressComponents;
            //alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
            //alert('您的位置：'+rs.point.lng+','+rs.point.lat);
        });
    });

    for(var i = 0; i < $scope.taskList.length; i++){
        (function(i){
            var taskPoint = new BMap.Point($scope.taskList[i].longitude,$scope.taskList[i].latitude);
            BMap.Convertor.translate(taskPoint, 0, function (point) {
                var one = userPositionFactory.getOne(point, $scope.taskList[i].user.headImage,i);
                map.addOverlay(one);
                $scope.markerList.push(one);
            });
        })(i);
    }

    map.addEventListener('touchstart',function(e){
        if(e.domEvent.srcElement.getAttribute("class").indexOf("user-position-img") >= 0){
            //先把所有头像取消选中，再对点击的头像进行选中操作
            for(var i = 0; i < $scope.markerList.length; i++){
                $scope.markerList[i].cancelSelect();
            }
            e.domEvent.srcElement.style.width = "60px";
            e.domEvent.srcElement.style.height = "60px";
            e.domEvent.srcElement.style.border = "2px #339999 solid";
            //从第18为开始截取class(user-position-img-index)中的index
            var index = e.domEvent.srcElement.getAttribute("class").substr(18,e.domEvent.srcElement.getAttribute("class").length);
            //alert("index="+index);
            //console.log($('li.search-task-wrapper')[index]);
            for(var i = 0; i < $('li.search-task-wrapper').length; i++){
                if(i == index){
                    $('li.search-task-wrapper')[index].style.display = "";
                } else {
                    $('li.search-task-wrapper')[i].style.display = "none";
                }
            }


        }
    });


}])


app.factory('userPositionFactory', [function() {

    function userPosition(point, headImage, i){
        this._point = point;
        this._headImage = headImage;
        this._index = i;
    }
    userPosition.prototype = new BMap.Overlay();

    userPosition.prototype.initialize = function(map){
        this._map = map;
        var div = this._div = document.createElement("div");
        div.style.position = "absolute";
        div.style.width = "50px";
        div.style.height = "50px";
        div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
        div.style.MozUserSelect = "none";
        div.setAttribute("class","search-user-img");
        var img = this._img = document.createElement("img");
        img.setAttribute("class","user-position-img-"+this._index);
        img.src=this._headImage;
        div.appendChild(img);

        map.getPanes().markerPane.appendChild(div);
        return div;
    }

    userPosition.prototype.draw = function(){
        // 根据地理坐标转换为像素坐标，并设置给容器
        var position = this._map.pointToOverlayPixel(this._point);
        this._div.style.left = position.x - 25 + "px";
        this._div.style.top = position.y - 25 + "px";
    }

    userPosition.prototype.cancelSelect = function(){
        this._img.style.height = "50px";
        this._img.style.width = "50px";
        this._img.style.border = "";
    }

    return {
        getOne:function(point,headImage, i){
            var one = new userPosition(point, headImage, i);
            return one;
        }
    }
}])
